#! /usr/bin/env python3.8
# -*- coding: UTF-8 -*-

# from loguru import logger

# level = 0  # 5: TRACE 10: DEBUG 20:INFO 25:SUCCESS 30:WARNING 40:ERROR 50:CRITICAL
# format="{time:YYYY-MM-DD HH:mm:ss.SSS} [{level.name[0]}] [{module} {function}:{line}] {message}"

# if level > 10:
#     logger.remove()
# logger.add("puwei.log.{time:YYYYMMDD}", rotation="00:00:00", retention="30 days", level=level, format=format)

import os
import logging
import logging.handlers


class Filter(logging.Filter):
    def filter(self, record):
        return not (record.funcName == "emit")


def loggerInit():
    logger = logging.getLogger()
    if logger.handlers:
        return logger

    os.makedirs("./data", 511, True)

    fmt = logging.Formatter("%(asctime)s [%(levelname)1.1s] [%(module)s %(funcName)s:%(lineno)d] %(message)s")
    fmt.default_msec_format = "%s.%03d"

    handler = logging.handlers.TimedRotatingFileHandler("./data/log", "MIDNIGHT", 1, 15)
    handler.setFormatter(fmt)

    logger.setLevel(logging.INFO)
    logger.addHandler(handler)

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(fmt)
    logger.addHandler(ch)
    logger.addFilter(Filter())
    return logger


logger = loggerInit()

if __name__ == "__main__":
    logger.debug("test")
    logger.info("test")
    logger.warning("test")
    logger.error("test %s", "asdsa===")
    logger.critical("test")
